package per.hx.bs.web.sys.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.utils.FileUpload;

import java.util.*;


/**
 * @author HuXing
 * @date 2018/3/30  10:59
 */
@Controller
@RequestMapping("/sys/file")
public class FileController extends BaseController {


    @PostMapping(value = "/fileUpload")
    @ResponseBody
    public ReturnDTO fileUpload(@RequestParam("file") MultipartFile[] files) {
        if (files != null && files.length > 0) {
            for (MultipartFile file : files) {
                try {
                    Long fId = FileUpload.saveFile(file, getUserId());
                    if (fId != 0L) {
                        return ReturnDTO.ok(fId);
                    }
                } catch (RuntimeException re) {
                    re.printStackTrace();
                }
            }
            return ReturnDTO.error("文件上传失败");
        }
        return ReturnDTO.error("文件为空");
    }

}
